using System;
using System.Threading;
using System.Collections;

namespace ActorSystem
{
	public class Actor
	{
		private ArrayList messages;
		//Class to represent an actor
		public Actor()
		{
			messages = new ArrayList();
		}
		
		public void AddMessage(Message msg)
		{
			messages.Add(msg);
		}
		
		public Control LocalControl { get; set; }
		
		public Id LocalId { get; set; }
		
		public void CreateId()
		{
			this.LocalId = new Id();
			this.LocalId.ActorIdentifier = this.GetHashCode();
			this.LocalId.Host = this.LocalControl.GetHostIp().ToString();
		}
		
		public void Send(Id to, Object contents) 
		{
			LocalControl.Send(to,this.LocalId,contents);
		}
		
		//Starts the actors work process, in a thread
		public void Start() 
		{
			Thread worker = new Thread(new ThreadStart(Work));
			worker.Start();
		}

		public virtual void Work()
		{
			
		}
		
		public Message ReadMessage()
		{
			//Dummy functions. More to come and better to come
			//with pattern matching and stuff
			while (messages.Count == 0)
				;
			return (Message)messages[0];
		}
		
		~Actor() 
		{
			//Tell the actorcontrol that this actor is dead/leaving the system
		}
		
	}
}
